package com.joker.orange.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.joker.common.core.page.TableDataInfo;
import com.joker.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.joker.common.utils.StringUtils;
import com.joker.orange.domain.NovelInfo;
import com.joker.orange.domain.bo.NovelInfoBo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.joker.orange.domain.bo.CaricatureInfoBo;
import com.joker.orange.domain.vo.CaricatureInfoVo;
import com.joker.orange.domain.CaricatureInfo;
import com.joker.orange.mapper.CaricatureInfoMapper;
import com.joker.orange.service.ICaricatureInfoService;

import java.util.*;

/**
 * 漫画信息Service业务层处理
 *
 * @author Joker
 * @date 2024-06-09
 */
@RequiredArgsConstructor
@Service
public class CaricatureInfoServiceImpl implements ICaricatureInfoService {

    private final CaricatureInfoMapper baseMapper;

    /**
     * 查询漫画信息
     */
    @Override
    public CaricatureInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    @Override
    public CaricatureInfo getCaricatureInfo(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 查询漫画信息列表
     */
    @Override
    public TableDataInfo<CaricatureInfoVo> queryPageList(CaricatureInfoBo bo, PageQuery pageQuery) {
        Page<CaricatureInfoVo> result = baseMapper.queryPageList(pageQuery.build(), this.buildPageQueryWrapper(bo));
        return TableDataInfo.build(result);
    }

    private Wrapper<CaricatureInfo> buildPageQueryWrapper(CaricatureInfoBo bo) {
        QueryWrapper<CaricatureInfo> wrapper = Wrappers.query();
        wrapper.eq(ObjectUtil.isNotNull(bo.getCategoryId()), "n.category_id", bo.getCategoryId())
                .like(StringUtils.isNotBlank(bo.getCaricatureName()), "n.novel_name", bo.getCaricatureName())
                .eq(StringUtils.isNotBlank(bo.getCaricatureLabel()), "n.novel_label", bo.getCaricatureLabel())
                .like(StringUtils.isNotBlank(bo.getCaricatureType()), "n.novel_type", bo.getCaricatureType());
        return wrapper;
    }

    /**
     * 查询漫画信息列表
     */
    @Override
    public List<CaricatureInfoVo> queryList(CaricatureInfoBo bo) {
        LambdaQueryWrapper<CaricatureInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CaricatureInfo> buildQueryWrapper(CaricatureInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CaricatureInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCategoryId() != null, CaricatureInfo::getCategoryId, bo.getCategoryId());
        lqw.eq(StringUtils.isNotBlank(bo.getCaricatureCoverCopy()), CaricatureInfo::getCaricatureCover, bo.getCaricatureCoverCopy());
        lqw.eq(StringUtils.isNotBlank(bo.getCaricatureName()), CaricatureInfo::getCaricatureName, bo.getCaricatureName());
        lqw.eq(StringUtils.isNotBlank(bo.getCurrentChapter()), CaricatureInfo::getCurrentChapter, bo.getCurrentChapter());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateChapter()), CaricatureInfo::getUpdateChapter, bo.getUpdateChapter());
        lqw.eq(StringUtils.isNotBlank(bo.getChapterTotal()), CaricatureInfo::getChapterTotal, bo.getChapterTotal());
        lqw.eq(StringUtils.isNotBlank(bo.getCaricatureIntroduce()), CaricatureInfo::getCaricatureIntroduce, bo.getCaricatureIntroduce());
        lqw.eq(StringUtils.isNotBlank(bo.getCaricatureLabel()), CaricatureInfo::getCaricatureLabel, bo.getCaricatureLabel());
        lqw.eq(StringUtils.isNotBlank(bo.getCaricatureType()), CaricatureInfo::getCaricatureType, bo.getCaricatureType());
        lqw.eq(bo.getLikeNum() != null, CaricatureInfo::getLikeNum, bo.getLikeNum());
        lqw.eq(bo.getCollectNum() != null, CaricatureInfo::getCollectNum, bo.getCollectNum());
        lqw.eq(bo.getHeatNum() != null, CaricatureInfo::getHeatNum, bo.getHeatNum());
        lqw.eq(bo.getReviewNum() != null, CaricatureInfo::getReviewNum, bo.getReviewNum());
        lqw.eq(StringUtils.isNotBlank(bo.getIsTop()), CaricatureInfo::getIsTop, bo.getIsTop());
        return lqw;
    }

    /**
     * 新增漫画信息
     */
    @Override
    public Boolean insertByBo(CaricatureInfoBo bo) {
        CaricatureInfo add = BeanUtil.toBean(bo, CaricatureInfo.class);
        validEntityBeforeSave(add, bo.getCaricatureCoverCopy());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Long insertCaricatureInfo(CaricatureInfo caricatureInfo) {
        baseMapper.insert(caricatureInfo);
        return caricatureInfo.getId();
    }

    /**
     * 修改漫画信息
     */
    @Override
    public Boolean updateByBo(CaricatureInfoBo bo) {
        CaricatureInfo update = BeanUtil.toBean(bo, CaricatureInfo.class);
        validEntityBeforeSave(update, bo.getCaricatureCoverCopy());
        return baseMapper.updateById(update) > 0;
    }

    @Override
    public void updateCaricatureChapterAndHeatNum(CaricatureInfoBo bo) {
        baseMapper.updateCaricatureChapterAndHeatNum(bo);
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CaricatureInfo entity, String caricatureCoverCopy){
        entity.setCaricatureCover(caricatureCoverCopy);
    }

    /**
     * 批量删除漫画信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<CaricatureInfoVo> getRandomCaricatures(List<CaricatureInfoVo> caricatureInfoVoList, int count) {
        // 返回空列表，如果输入无效
        if (caricatureInfoVoList == null || caricatureInfoVoList.size() < count || count <= 0) {
            return new ArrayList<>();
        }
        if (caricatureInfoVoList.size() <= count) {
            return caricatureInfoVoList;
        }

        // 复制列表以避免改变原始列表
        List<CaricatureInfoVo> shuffledList = new ArrayList<>(caricatureInfoVoList);

        // 打乱列表顺序
        Collections.shuffle(shuffledList);

        // 截取前count个元素作为随机选取的结果
        return shuffledList.subList(0, Math.min(count, shuffledList.size()));
    }

    @Override
    public List<CaricatureInfo> listAllCaricature() {
        return baseMapper.selectList();
    }

}
